package com.yvon.maple.gateway.properties;

import cn.hutool.core.util.ArrayUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : Yvon
 * @since : 2021-08-11
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Component
@ConfigurationProperties(prefix="maple.security")
public class IgnoreProperties {

    private Boolean enableAuthentication = true;

    private List<String> ignoreUrls = new ArrayList<>();


    /**
     * 是否忽略令牌
     *
     * @param path 路径
     * @return boolean
     * @author : Yvon / 2022-03-16
     */
    public boolean isIgnoreToken(String path) {
        PathMatcher pathMatcher = new AntPathMatcher();
        List<String> all = new ArrayList<>(getIgnoreUrls());
        return all.stream().anyMatch(url -> {
            if (enableAuthentication) {
                return pathMatcher.match(url, path);
            }
            return true;
        });
    }

    public String[] getIgnoreUrl() {
        if (!enableAuthentication) {
            return new String[] {"/**"};
        }
        return ArrayUtil.toArray(getIgnoreUrls(), String.class);
    }
}
